package com.atguigu.base;

import com.atguigu.util.CastUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * @PACKAGE_NAME: com.atguigu.base
 * @CLASSNAME: BaseServiceImpl
 * @AUTHOR: zhangsan
 * @DATE: 2024/4/16 19:47
 * @SINCE 17.0.7
 * @DESCRIPTION: BaseServiceImpl
 */
public abstract class BaseServiceImpl<T> implements BaseService<T>{

    protected abstract BaseDAO<T> getEntityDAO();

    /**
     * 新增角色
     *
     */
    @Override
    public Integer insert(T t) {
        Integer i = getEntityDAO().insert(t);
        return i;
    }

    @Override
    public T getById(Serializable id) {
        T t = getEntityDAO().getById(id);
        return t;
    }

    @Override
    public Integer update(T t) {
        Integer i = getEntityDAO().update(t);
        return i;
    }


    @Override
    public Integer delete(Serializable id) {
        Integer i = getEntityDAO().delete(id);
        return i;
    }

    @Override
    public PageInfo<T> findPage(Map<String, Object> filters) {
        //开启分页插件
        int pageNum = CastUtil.castInt(filters.get("pageNum"),1);
        int pageSize = CastUtil.castInt(filters.get("pageSize"),10);

        PageHelper.startPage(pageNum, pageSize);
        //调用DAO层获取分页的role数据和分页数据
        Page<T> page = getEntityDAO().findPage(filters);
        //将Page转换为PageInfo并返回
        PageInfo<T> rolePageInfo = new PageInfo<>(page);

        return rolePageInfo;
    }
}
